package cn.edu.anan.controller;

import cn.edu.anan.rule.MySentinelResource;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * sentinel controller
 * </p>
 *
 * @author yanghouhua@edu.com.cn
 * @since 2021/8/6
 */
@RestController
@RequestMapping("sentinel")
@Slf4j
public class SentinelController {

    /**
     * 测试熔断降级
     * @param userId
     * @return
     */
    @RequestMapping("grade/{userId}")
    public String testGrade(@PathVariable("userId") String userId){

        log.info("SentinelController---testGrade......用户Id：{}",userId);

        return "test grade ok!";
    }

    /**
     * 测试限流
     * @return
     */
    @RequestMapping("test")
    @SentinelResource(value = MySentinelResource.FLOW_RESOURCE, blockHandler = "testBlockHandler")
    public String test(){
        log.info("SentinelController---test......");

        return "test ok !";
    }

    /**
     * 限流后处理方法
     * @param e
     * @return
     */
    public String testBlockHandler(BlockException e){
        log.error("限流异常了：{}", e);
        return "error，限流了！";
    }
}
